﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MyVoteMVC.Data;
using MyVoteMVC.Domain;
using MyVoteMVC.Common;
using MyVoteMVC.Common.Objects;
using MyVoteMVC.Data.Repositories;

namespace MyVoteMVC.Service
{
    public class VoteMessageService : IMessageService
    { 
        private const string EmailAddress = "@gzdi.com";//接收方邮箱    
        IUnitOfWork unitOfWork;
        IRepository<VT_Messages> messagesRepository;

        public VoteMessageService()
        {
            unitOfWork = Factory.GetInstance<IUnitOfWork>();
            messagesRepository = unitOfWork.GetRepository<VT_Messages>();
        }
        #region VoteMessageService Members

        //创建
        public bool CreateMessage(string activityID, string messageTitle, string messageContent, string userEmailName, out string MessageId)
        {
            MessageId = "";
            //userEmailName = userEmailName + EmailAddress;          
            VT_Messages v_messages = new VT_Messages();
            unitOfWork.BeginTransaction();
            v_messages.Activity_ID = activityID;
            v_messages.Created_By = userEmailName;
            v_messages.Created_Date = DateTime.Now;
            v_messages.Message_Content = messageContent;
            v_messages.Message_ID = Guid.NewGuid().ToString();
            int msx = Convert.ToInt32((from activity in messagesRepository.QueryableEntity()
                                       where activity.Activity_ID == activityID
                                       select activity.Message_No).Max());
            v_messages.Message_No = msx + 1;   
            v_messages.Message_Title = messageTitle;
            messagesRepository.Add(v_messages);
            unitOfWork.CommitTransaction();
            MessageId = v_messages.Message_ID;
            return true;
        }

        //编辑
        public bool EditMessage(string activityID, string messageID, string messageTitle, string messageContent, string userEmailName)
        {
            //userEmailName = userEmailName + EmailAddress;
            VT_Messages v_messages = messagesRepository.GetMany(o => o.Activity_ID == activityID && o.Message_ID == messageID).FirstOrDefault();
            if (v_messages != null)
            {
                unitOfWork.BeginTransaction();
                v_messages.Message_Title = messageTitle;
                v_messages.Message_Content = messageContent;
                v_messages.Last_Update_By = userEmailName;
                v_messages.Last_Update_Date = DateTime.Now;
                messagesRepository.Update(v_messages);
                unitOfWork.CommitTransaction();
                return true;
            }
            return false;
            
        }

        //删除
        public bool DeleteMessage(string activityID, string messageID)
        {
            VT_Messages v_messages = messagesRepository.GetMany(o => o.Activity_ID == activityID && o.Message_ID == messageID).FirstOrDefault();
            if (v_messages != null)
            {
                unitOfWork.BeginTransaction();
                messagesRepository.Delete(v_messages);
                unitOfWork.CommitTransaction();
                return true;
            }
            return false;
        }

        // 模糊查找

        public IList<VT_Messages> FindMessageList(string strContent)
        {
            IList<VT_Messages> messages = messagesRepository.GetMany(o => o.Message_Content.Contains(strContent) || o.Message_Title.Contains(strContent)).OrderBy(o => o.Message_No).ToList();
            return messages;
        }

        //获取
        public IList<VT_Messages> GetMessageList(string activityID)
        {
            IList<VT_Messages> messages = messagesRepository.GetMany(o => o.Activity_ID == activityID).OrderBy(o=>o.Message_No).ToList();
            return messages;
        }

        public IList<VT_Messages> GetMessageList()
        {
            IList<VT_Messages> messages = messagesRepository.GetAll().ToList();
            return messages;
        }

        public IList<VT_Messages> GetMessageListByInterval(string activityID,double timeSpan)
        {
            timeSpan = 0 - timeSpan;
            DateTime dt = DateTime.Now.AddMinutes(timeSpan);
            IList<VT_Messages> messages = messagesRepository.GetMany(o => o.Activity_ID == activityID && (o.Created_Date >= dt||o.Last_Update_Date>=dt)).OrderBy(o => o.Message_No).ToList();
            return messages;
        }

        public IList<VT_Messages> GetMessageListByInterval(double timeSpan)
        {
            timeSpan = 0 - timeSpan;
            DateTime dt = DateTime.Now.AddMinutes(timeSpan);
            IList<VT_Messages> messages = messagesRepository.GetMany(o => o.Created_Date >= dt || o.Last_Update_Date >= dt).OrderBy(o => o.Message_No).ToList();
            return messages;
        }
        #endregion

        #region IDisposable Members

        private bool m_disposed;
        protected virtual void Dispose(bool disposing)
        {
            if (!m_disposed)
            {
                if (disposing)
                {
                    unitOfWork.Dispose();   
                }

                // Release unmanaged resources

                m_disposed = true;
            }
        }

        public void Dispose()
        {
            Dispose(true);
            GC.SuppressFinalize(this);
        }

        ~VoteMessageService()
        {
            Dispose(false);
        }
        #endregion
    }
}
